/**
 * Created by Handsome.Ho on 2015/10/13.
 */
angular.module('cloud.controllers', []).controller('cloudCtrl', function ($scope, $rootScope, cloud, $ionicActionSheet, $ionicPopup, $timeout, TaskService) {
  $rootScope.account = null;
  $scope.username = null;
  $scope.password = null;

  $scope.loadFromCloud = function () {
    TaskService.loadFromCloud($rootScope.account.loginid);
  }

  $scope.login = function (username, password) {
    cloud.validate(username, password, function (data) {
      $rootScope.account = data;
    }, function (data) {
      var popup = $ionicPopup.show({
        title: '登录失败',
        template: data
      });
      $timeout(function () {
        popup.close();
      }, 2000);
    });
  }

  $scope.logout = function () {
    $rootScope.account = null;
  }

  $scope.show = function () {
    // 显示操作表
    $ionicActionSheet.show({
      destructiveText: '注销',
      titleText: '注销用户将导致未推送到云端的日程信息丢失，请确定是否注销用户?',
      cancelText: '取消',
      destructiveButtonClicked: function () {
        $scope.logout();
        return true;
      }
    });

  };
});
